export function formatDate (date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + ''
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
    }
  }
  return fmt
}

function padLeftZero (str) {
  return ('00' + str).substr(str.length)
}

export function shijianchuo () {
    var tmp = new Date().getTime();
    var strdate = tmp.toString() 
    var date = strdate.substring(0,strdate.length-3);
    return date+"000"
}


export function timetostamp (date) {
    // var tmp = new Date().getTime();
    // var strdate = tmp.toString() 
    // if t.substring(a.length-3){
    //   var date = t.substring(0,t.length-3);
    // }
    // var date = '2015-03-05 17:59:00';
    date = date.substring(0,19);    
    date = date.replace(/-/g,'/'); //必须把日期'-'转为'/'
    var timestamp = new Date(date).getTime();
    // document.write(timestamp);
    return timestamp
}


export function stamptotime (timestamp) {

    // var   timestamp = timestamp.toString() 
    // if (timestamp.substring(timestamp.length-3) == "000"){
    //   timestamp = timestamp.substring(0,timestamp.length-3);
    // }
    // console.log(timestamp);

    var d = new Date(timestamp);    //根据时间戳生成的时间对象
    console.log(timestamp,d);
    var date = (d.getFullYear()) + "-" +(d.getMonth() + 1) + "-" +(d.getDate()) + " " + (d.getHours()) + ":" + (d.getMinutes()) + ":" + (d.getSeconds());
    return date
}


export function Sleep(time){
    var start = new Date().getTime() + parseInt(time,10);
    while(new Date().getTime() <  start ) {

    }
    return 
}